package com.wlhse.controller;

import com.wlhse.dao.EmployeeManagementDao;
import com.wlhse.dto.inDto.ProblemImportInDto;
import com.wlhse.dto.outDto.ProblemImportDto;
import com.wlhse.service.ProblemImportService;
import com.wlhse.util.GetCurrentUserIdUtil;
import com.wlhse.util.R;
import com.wlhse.util.state_code.CodeDict;
import com.wlhse.util.state_code.NR;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController("ProblemController")
@RequestMapping("/api/v3")
public class ProblemController {

    @Resource
    private ProblemImportService problemImportService;

    @Resource
    private EmployeeManagementDao employeeManagementDao;

    @Resource
    private GetCurrentUserIdUtil getCurrentUserIdUtil;

    //问题整改
    @RequestMapping(value = "/rect_problems/{id}", method = RequestMethod.PUT, produces = "application/json; charset=utf-8")
    public String updateRectProblemImportDto(@RequestBody(required = false) ProblemImportDto problemImportDto, @PathVariable("id") int id, HttpServletRequest request) {
        problemImportDto.setProblemID(id);
        return problemImportService.updateRectProblemImport(problemImportDto, getCurrentUserIdUtil.getUserId(request));
    }

    //问题验证
    @RequestMapping(value = "/verify_problems/{id}", method = RequestMethod.PUT, produces = "application/json; charset=utf-8")
    public String updateVerifyProblemImportDto(@RequestBody(required = false) ProblemImportDto problemImportDto, @PathVariable("id") int id, HttpServletRequest request) {
        problemImportDto.setProblemID(id);
        return problemImportService.updateVerifyProblemImport(problemImportDto,getCurrentUserIdUtil.getUserId(request));
    }

    @RequestMapping(value = "/problems", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public String addProblemImportDto(@RequestBody(required = false) ProblemImportDto problemImportDto) {
        problemImportService.insertProblemImport(problemImportDto);
        return NR.r(CodeDict.CODE_MESSAGE, 1, 0, null, null, 0, 0);
    }

    @RequestMapping(value = "/problems", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String selectAllProblemImportDto(@ModelAttribute ProblemImportInDto problemImportInDto, HttpServletRequest request) {
        if (StringUtils.isEmpty(problemImportInDto.getCompanyCode())) {
            String companyCode = employeeManagementDao.querySecondCompanyCodeByEmpId(getCurrentUserIdUtil.getUserId(request));
            problemImportInDto.setCompanyCode(companyCode);
        }
        return problemImportService.queryProblemImport(problemImportInDto);
    }

    @RequestMapping(value = "/problems/{id}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String selectSingleProblemImportDto(@PathVariable("id") int id) {
        return problemImportService.queryProblemImportById(id, null);
    }

    @RequestMapping(value = "/my_problems", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String selectMyProblemImportDto(@ModelAttribute ProblemImportInDto problemImportInDto, HttpServletRequest request) {
        problemImportInDto.setCheckPersonID(getCurrentUserIdUtil.getUserId(request));
        return problemImportService.queryProblemImport(problemImportInDto);
    }

    @RequestMapping(value = "/my_problems/{id}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String selectMySingleProblemImportDto(@PathVariable("id") int id, HttpServletRequest request) {
        return problemImportService.queryProblemImportById(id, getCurrentUserIdUtil.getUserId(request));
    }

    //问题录入
    @RequestMapping(value = "/problems_add", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public R newAddProblemImportDto(@RequestBody(required = false) ProblemImportDto problemImportDto, HttpServletRequest request) {
        return problemImportService.newInsertProblemImport(problemImportDto,getCurrentUserIdUtil.getUserId(request));
    }

    @RequestMapping(value = "/problems/{problem_id}", method = RequestMethod.DELETE, produces = "application/json; charset=utf-8")
    public String deleteProblem(@PathVariable("problem_id") int problem_id) {
        return problemImportService.deleteProblem(problem_id);
    }

    @RequestMapping(value = "/problem_code/{id}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String getUpdateProblem(@PathVariable("id") Integer id) {
        return problemImportService.getModificationById(id);
    }

    @RequestMapping(value = "/problem_code", method = RequestMethod.PUT, produces = "application/json; charset=utf-8")
    public String updateProblemImportDto(@RequestBody(required = false) ProblemImportDto problemImportDto) {
        return problemImportService.updateProblemImportDto(problemImportDto);
    }

    @RequestMapping(value = "/problem/{id}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String getUpdateProblemById(@PathVariable("id") Integer id) {
        return problemImportService.getUpdateProblemById(id);
    }

    //问题验证
    @RequestMapping(value = "/problems/{id}", method = RequestMethod.PUT, produces = "application/json; charset=utf-8")
    public String updateProblemImportDto(@RequestBody(required = false) ProblemImportDto problemImportDto, @PathVariable("id") int id,HttpServletRequest request) {
        problemImportDto.setProblemID(id);
        return problemImportService.updateProblemImport(problemImportDto,getCurrentUserIdUtil.getUserId(request));
    }

    @RequestMapping(value = "/problem_condition", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String selectSomeProblem(@ModelAttribute ProblemImportInDto problemImportInDto) {
        return problemImportService.selectSomeProblem(problemImportInDto);
    }

    @RequestMapping(value = "/problem_by_status_my", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String getProblemImportDtoByStatus2(@ModelAttribute ProblemImportInDto problemImportInDto,HttpServletRequest request) {
        Integer userId = getCurrentUserIdUtil.getUserId(request);
        problemImportInDto.setResponsePersonID(userId);
        return problemImportService.getProblemImportDtoByStatus2(problemImportInDto);
    }

    @RequestMapping(value = "/problem_by_status", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String getProblemImportDtoByStatus3(@ModelAttribute ProblemImportInDto problemImportInDto) {
        return problemImportService.getProblemImportDtoByStatus2(problemImportInDto);
    }

    @RequestMapping(value = "/problems_my_company", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String selectAllProblemImportDtoMyCompany(@ModelAttribute ProblemImportInDto problemImportInDto, HttpServletRequest request) {
        Integer userId = getCurrentUserIdUtil.getUserId(request);
        problemImportInDto.setCompanyCode(employeeManagementDao.queryCompanyCodeByEmpId(userId));
        return problemImportService.queryProblemImport(problemImportInDto);
    }
}
